<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\Category;
use App\Models\Good;
use App\Models\GoodInfo;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    public function all(Request $request)
    {
        $limit = $request->get('limit', config('common.pagination.limit'));

        $items = Article::select(['id', 'author', 'good_id', 'category_id', 'title'])
            ->paginate($limit);
        return jsonResponse(config('common.successCode'), $items);
    }

    public function create(Request $request)
    {
        $user = checkUserLoginStatus($request);
        $goodId = $request->get('good');
        if (empty($goodId)) {
            $categoryId = $request->get('category');
            $category = Category::find($categoryId);
            if (empty($category)) {
                return jsonResponse(40000);
            }
            $title = $request->get('title');
            if (empty($title) || strlen($title) == 0) {
                return jsonResponse(41500);
            }
        } else {
            $goodInfo = GoodInfo::find($goodId);
            if (empty($goodInfo)) {
                return jsonResponse(40000);
            }
            $good = Good::find($goodInfo['good_id']);
            $categoryId = $good['category_id'];
            $title = $good['title'];
        }
        $content = $request->get('content');
        if (empty($content) || strlen($content) == 0) {
            return jsonResponse(41500);
        }

        $article = new Article();
        $article['author'] = $user['id'];
        $article['good_id'] = $goodId;
        $article['category_id'] = $categoryId;
        $article['title'] = $title;
        $article['content'] = $content;
        $article->save();
        return jsonResponse();
    }

    public function update(Request $request, $id)
    {
        $user = checkUserLoginStatus($request);
        $goodId = $request->get('good');
        if (empty($goodId)) {
            $categoryId = $request->get('category');
            $category = Category::find($categoryId);
            if (empty($category)) {
                return jsonResponse(40000);
            }
            $title = $request->get('title');
            if (empty($title) || strlen($title) == 0) {
                return jsonResponse(41500);
            }
        } else {
            $goodInfo = GoodInfo::find($goodId);
            if (empty($goodInfo)) {
                return jsonResponse(40000);
            }
            $good = Good::find($goodInfo['good_id']);
            $categoryId = $good['category_id'];
            $title = $good['title'];
        }
        $content = $request->get('content');
        if (empty($content) || strlen($content) == 0) {
            return jsonResponse(41500);
        }

        $article = Article::find($id);
        if (empty($article)) {
            return jsonResponse(40000);
        }
        $article['author'] = $user['id'];
        $article['good_id'] = $goodId;
        $article['category_id'] = $categoryId;
        $article['title'] = $title;
        $article['content'] = $content;
        $article->save();
        return jsonResponse();
    }

    public function destroy($id)
    {
        Article::destroy($id);
        return jsonResponse();
    }
}
